<?php
/**
 * Description of user
 *
 * @author aquilax
 */

require 'FE_Controller.php';

class User extends FE_Controller{

  function  __construct() {
    parent::__construct();
    $this->user_model->forum_id = $this->forum_id;
    $this->data['path'][''] = lang('Home');
  }

  function login(){
    if ($this->logged){
      //User is already logged in
      redirect('');
    }
    
    $this->data['title'] = lang('Login');
    $this->data['path']['user/login'] = lang('Login');

    $this->load->helper('forum');
    $this->load->library('form_validation');
    
    $this->form_validation->set_rules('username', lang('Username'), 'trim|xss_clean|required|min_length[3]|max_length[30]');
    $this->form_validation->set_rules('password', lang('Password'), 'trim|required|min_length[3]|myhash');

		if ($this->form_validation->run() == TRUE){
      if ($this->user_model->login()){
        redirect('', 'refresh');
      } else {
        $this->form_validation->_error_array[] = lang('Bad User Name or Password');
      }
		}
    $this->render();
  }

  function logout(){
    $this->user_model->logout();
    redirect('');
  }

  function profile(){
    $user_id = $this->uri->segment(3);
    if (!$user_id){
      //FIXME:Error bad argument
    }
    $this->load->model('huser_model');
    $this->huser_model->forum_id = $this->forum_id;
    $user = $this->huser_model->loadUser($user_id);
    if (!$user){
      //FIXME:Error user not found
    }
    $this->load->helper('forum');
    $this->data['path']['user/profile/'.$user['user_id']] = $user['username'];
    $this->data['user'] = $user;
    $this->render();
  }
}
?>
